#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QPen>
#include <QBrush>
#include <QRect>
#include <QScreen>
#include <QPalette>
#include <QPixmap>
#include <QRadialGradient>
#include <QLinearGradient>
#include <QConicalGradient>
#include <QImage>
#include <QLine>
#include <Qvector>
#include <QPainterPath>
#include <QPoint>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 或者，如果你想先将窗口设置为居中，然后再最大化
    // 首先计算屏幕中心点的位置
    QRect screenGeometry = QApplication::primaryScreen()->availableGeometry();
    int x = (screenGeometry.width() - this->width()) / 2;
    int y = (screenGeometry.height() - this->height()) / 2;

    // 将窗口移动到屏幕中心
    this->move(x, y);

    // 然后最大化窗口
    this->showMaximized();
    // 设置窗口为白色背景
    setPalette(QPalette(Qt::white));
    setAutoFillBackground(true);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    // 1.设置单色画刷填充矩形
    // drawFillRect();
    // 2.设置材质图片填充矩形
    // drawPixmapFillRect();
    // 3.绘制辐射渐变填充
    drawRadialGradientFillRect();
    // 4.绘制线性渐变
    // drawLinearGradientFillRect();
    // 5.绘制圆锥形渐变
    // drawConicalGradientFillRect();
    // 绘制基本图形
    // drawBasicShape();
}

void MainWindow::drawFillRect()
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::TextAntialiasing);
    // 绘图区宽度
    int nW = width();
    // 绘图区高度
    int nH = height();
    // 中间区域矩形框
    QRect rect(nW/4,nH/4,nW/2,nH/2);

    QPen pen;
    pen.setWidth(3);
    pen.setColor(Qt::red);
    // 线的样式、实线、虚线等
    pen.setStyle(Qt::SolidLine);
    // 线端点样式
    pen.setCapStyle(Qt::FlatCap);
    // 线的连接点样式
    pen.setJoinStyle(Qt::BevelJoin);
    painter.setPen(pen);
    // 设置画刷
    QBrush brush;
    // 设置画刷颜色
    brush.setColor(Qt::yellow);
    // 画刷填充样式
    brush.setStyle(Qt::SolidPattern);
    painter.setBrush(brush);
    // 绘图
    painter.drawRect(rect);
}

void MainWindow::drawPixmapFillRect()
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::TextAntialiasing);
    // 绘图区宽度
    int nW = width();
    // 绘图区高度
    int nH = height();
    // 中间区域矩形框
    QRect rect(nW/4,nH/4,nW/2,nH/2);

    QPen pen;
    pen.setWidth(3);
    pen.setColor(Qt::red);
    // 线的样式、实线、虚线等
    pen.setStyle(Qt::SolidLine);
    // 线端点样式
    pen.setCapStyle(Qt::FlatCap);
    // 线的连接点样式
    pen.setJoinStyle(Qt::BevelJoin);
    painter.setPen(pen);

    QPixmap pixmap(":Images/Image/texture.jpg");
    QBrush brush;
    // 画刷填充样式
    brush.setStyle(Qt::TexturePattern);
    // 设置材质图片
    brush.setTexture(pixmap);
    painter.setBrush(brush);
    // 绘图
    painter.drawRect(rect);

}

void MainWindow::drawRadialGradientFillRect()
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::TextAntialiasing);
    // 绘图区宽度
    int nW = width();
    // 绘图区高度
    int nH = height();
    // 径向渐变
    QRadialGradient radialgradient(nW/2,nH/2,qMax(nW/8,nH/8),nW/2,nH/2);
    radialgradient.setColorAt(0,QColor(Qt::green));
    radialgradient.setColorAt(1,QColor(Qt::blue));
    radialgradient.setSpread(QGradient::ReflectSpread);
    painter.setBrush(radialgradient);
    // 绘图,填充更大区域，会有延展效果
    painter.drawRect(this->rect());
}

void MainWindow::drawLinearGradientFillRect()
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::TextAntialiasing);
    // 线性渐变
    QLinearGradient lineargradient(rect().left(),rect().top(),rect().right(),rect().bottom());
    lineargradient.setColorAt(0,QColor(Qt::blue));
    lineargradient.setColorAt(0.5,QColor(Qt::green));
    lineargradient.setColorAt(1,QColor(Qt::red));
    lineargradient.setSpread(QGradient::ReflectSpread);
    painter.setBrush(lineargradient);
    // 绘图,填充更大区域，会有延展效果
    painter.drawRect(this->rect());
}

void MainWindow::drawConicalGradientFillRect()
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::TextAntialiasing);
    // 绘图区宽度
    int nW = width();
    // 绘图区高度
    int nH = height();
    // 线性渐变
    QConicalGradient conicalgradient(nW/2,nH/2,45);
    conicalgradient.setColorAt(0,QColor(Qt::yellow));
    conicalgradient.setColorAt(0.5,QColor(Qt::blue));
    conicalgradient.setColorAt(1,QColor(Qt::green));
    painter.setBrush(conicalgradient);
    // 绘图,填充更大区域，会有延展效果
    painter.drawRect(this->rect());

}

void MainWindow::drawBasicShape()
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::TextAntialiasing);

    // 绘图区宽度
    int nW = width();
    // 绘图区高度
    int nH = height();
    QRect rect0(0,0,100,100);
    // 起始90°
    int nStartAngle = 90*16;
    // 旋转90°
    int nRotate = 90*16;
    // 绘制曲线
    painter.drawArc(rect0,nStartAngle,nRotate);
    // 绘制一段弦
    QRect rect1(100 ,0,100,100);
    painter.drawChord(rect1,nStartAngle,nRotate);
    // 根据给定的点画凸多边形
    QPoint pts[] = {
        QPoint(5*nW/12,nH/4),
        QPoint(3*nW/4,5*nH/12),
        QPoint(5*nW/12,3*nH/4),
        QPoint(nW/4,5*nH/12)
    };
    painter.drawConvexPolygon(pts,4);

    // 绘制椭圆
    QRect rect2(200,0,100,50);
    painter.drawEllipse(rect2);
    // 在指定的区域内绘制图片
    QRect rect3(300,0,100,100);
    QImage image(":Images/Image/qt.jpg");
    painter.drawImage(rect3,image);
    // 画直线
    QLine line(400,0,500,100);
    painter.drawLine(line);
    // 画一批直线
    QRect rect4(500,0,100,100);
    QVector<QLine> vtLines;
    vtLines.append(QLine(rect4.topLeft(),rect4.bottomRight()));
    vtLines.append(QLine(rect4.topRight(),rect4.bottomLeft()));
    vtLines.append(QLine(rect4.topLeft(),rect4.bottomLeft()));
    vtLines.append(QLine(rect4.topRight(),rect4.bottomRight()));
    painter.drawLines(vtLines);
    // 绘制由QPainterPath对象定义的路线
    QRect rect5(605,0,100,100);
    QPainterPath path;
    path.addEllipse(rect5);
    path.addRect(rect5);
    painter.drawPath(path);
    // 绘制扇形
    QRect rect6(705,0,100,100);
    int nStartAngle1 = 40*16;
    int nRotate1 = 120*16;
    painter.drawPie(rect6,nStartAngle1,nRotate1);
    // 绘制Pixmap图片
    QRect rect7(805,0,100,100);
    QPixmap pixmap(":Images/Image/qt.jpg");
    painter.drawPixmap(rect7,pixmap);
    // 画一个点
    painter.drawPoint(QPoint(855,0));
    // 画一批点
    QPoint points[]= {
        QPoint(5*nW/12,nH/4),
        QPoint(3*nW/4,5*nH/12),
        QPoint(2*nW/4,5*nH/12)
    };
    painter.drawPoints(points,3);
    // 绘制多变形
    QPoint pts2[]= {
        QPoint(5*nW/12,nH/4),
        QPoint(3*nW/4,5*nH/12),
        QPoint(5*nW/12,3*nH/4),
        QPoint(2*nW/4,5*nH/12)
    };
    painter.drawPolygon(pts2,4);
    // 画多点连接的线，最后一个点不会和第一个点连接
    QPoint pts3[]= {
        QPoint(5*nW/12,nH/4),
        QPoint(3*nW/4,5*nH/12),
        QPoint(5*nW/12,3*nH/4),
        QPoint(2*nW/4,5*nH/12),
    };
    painter.drawPolyline(pts3,4);
    // 绘制圆角矩形
    QRect rect8(900,0,100,50);
    painter.drawRoundedRect(rect8,20,20);
    // 绘制文本,只能绘制单行文本，字体的大小等属性由QPainter::font()决定
    QRect rect9(1000,0,100,100);
    QFont font;
    font.setPointSize(30);
    font.setBold(true);
    painter.setFont(font);
    painter.drawText(rect9,"Hello Qt");
    // 擦除某个区域，等效于用背景色填充该区域
    QRect rect10(1100,0,100,100);
    painter.eraseRect(rect10);
    // 填充某个QPainterPath定义的绘图路径,但是轮廓线不显示
    QRect rect11(0,200,100,100);
    QPainterPath path1;
    path1.addEllipse(rect11);
    path1.addRect(rect11);
    painter.fillPath(path1,Qt::red);
    // 填充一个矩形,无边框线
    QRect rect12(100,200,100,100);
    painter.fillRect(rect12,Qt::green);
}
